// pages/home/filter/filter.js
const app = getApp()
var util = require("../../../utils/util.js")
const db = wx.cloud.database()
const _ = db.command
const labelDB=db.collection('labelData')
const collegeDB=db.collection('collegeData')
var allisSelected = true   //选择全校以后其他将不会在被选择
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userOpenid: '', //用户openid
    labelArray:[], //标签 数组
    academyArray:[],//学院数组
    label_opacity:[], //标签透明度数组
    academy_opacity:[],//学院透明度数组
    label_selected:[],//选择的标签数组->作为筛选条件
    academy_selected:[]//选择的学院数组->作为筛选条件
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log("得到的栏目序号：",options.col)
    let that = this
    let colNum = options.col
    that.setLabelArray(colNum)
    that.setacademyArray("长江大学")
  },
  onShow:function(options){
    allisSelected=true
  },

  //添加了此方法  append
  onShow:function(options){
    allisSelected = true
  },
  
//通过栏目获取所有标签 
setLabelArray:function(col){
  let that = this
  labelDB.where({
    col:Number(col)
  }).get({
    success:function(res){
      that.setData({
        labelArray:res.data
      })
      console.log("所有标签:",that.data.labelArray)
      let label_length = that.data.labelArray.length
      for(var i=0;i<label_length;i++){
        that.data.label_opacity.push(0.5)
      }
      console.log(that.data.label_opacity)
    }
  })
},
//通过大学获取所有专业
setacademyArray:function(college){
  let that = this
  collegeDB.where({
    college:college
  }).get({
    success:function(res){ 
      that.setData({
        academyArray:res.data[0].allAcademy,
      })
      console.log(college,"的所有专业",that.data.academyArray)
      let academy_length = that.data.academyArray.length
      for(var i=0;i<academy_length;i++){
        that.data.academy_opacity.push(0.5)
      }
      console.log(that.data.academy_opacity)
    }
  })
},

 
  //点击每一个学院标签
  lighttheAcademy:function(e){
    console.log(e.target.id)
    let itemID = Number(e.target.id)
    let that=this
    let academy_opacity_item =  that.data.academy_opacity[itemID]
    let arraylength = that.data.academy_opacity.length
    if(0==itemID){  //选择全校
      if(0.5 == academy_opacity_item){
        that.data.academy_opacity[itemID] = 1
        //其他熄灭其他选择
        for(var i=1;i<=arraylength;i++){
          that.data.academy_opacity[i] = 0.5
        }
        that.data.academy_selected = []
        allisSelected = false
        // that.data.academy_selected.push(Number(itemID))
      }else{ 
        that.data.academy_opacity[itemID] = 0.5
        allisSelected = true
        // that.data.academy_selected.pop(Number(itemID))
      }
    }
    else{ //没有选择全校
      if(allisSelected){
        if(0.5 == academy_opacity_item){
          that.data.academy_opacity[itemID] = 1
          that.data.academy_selected.push(itemID)
        }else{
          that.data.academy_opacity[itemID] = 0.5
          that.data.academy_selected.splice(that.data.academy_selected.findIndex(e => e === itemID), 1)
        }
    }else{
      wx.showToast({
        title: '你已选择了全校范围',
        icon:"none"
      })
    }
  }
  that.setData({
    academy_opacity:that.data.academy_opacity
  })
    console.log(that.data.academy_opacity,that.data.academy_selected)
  },


    //点击每一个标签
    lighttheLabel:function(e){
      console.log(e.target.id)
      let itemID = Number(e.target.id)
      let that=this
      let label_opacity_item =  that.data.label_opacity[itemID]
      if(0.5 == label_opacity_item){
        that.data.label_opacity[itemID] = 1
        that.data.label_selected.push(itemID)
      }else{
        that.data.label_opacity[itemID] = 0.5
        that.data.label_selected.splice(that.data.label_selected.findIndex(e => e === itemID), 1)
      }
      that.setData({
        label_opacity:that.data.label_opacity
      })
      console.log(that.data.label_opacity)
    },


  //点击确定按钮
  clickBtnSure:function(){
    let that = this
    console.log(that.data.label_selected,that.data.academy_selected)
    var pages = getCurrentPages()
    var beforePage = pages[pages.length-2] 
    beforePage.setData({
      academyArray:that.data.academy_selected,
      labelArray:that.data.label_selected,
    })
    beforePage.changeData()
     wx.navigateBack({
     })
  }
})